package com.dxq.test.common;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.dxq.test.App;
import com.dxq.test.bean.RemindEntity;
import com.dxq.test.db.Breed;
import com.dxq.test.db.Group;
import com.dxq.test.db.Label;
import com.dxq.test.db.LabelBreed;
import com.dxq.test.db.Pasture;
import com.dxq.test.db.TablesDao;
import com.dxq.test.db.Vaccine;
import com.j256.ormlite.dao.ForeignCollection;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author: zsh
 * Date: 2023/12/6
 * Describe: 查询整理数据库数据
 */
public class DBOperate {

    private static String[] types = {"牛", "羊", "禽类"};

    /**
     * 获取提醒信息
     * @param tablesDao
     * @return
     */
    public static List<RemindEntity> getRemindList(TablesDao tablesDao) {
        List<RemindEntity> recordList = new ArrayList<>();
        Pasture pasture = tablesDao.queryPastureById(App.select_pasture_id);
        if (null != pasture) {
            ForeignCollection<Breed> breeds = pasture.getBreeds();
            if (!ObjectUtils.isEmpty(breeds)) {
                for (Breed breed : breeds) {
                    //创建StringBuffer对象
                    StringBuffer info_b = new StringBuffer("请记得为");//预产
                    StringBuffer info_n = new StringBuffer("请记得为");//配种
                    List<Label> labels_w = tablesDao.queryLabelForBreed(breed, LabelBreed.LABEL_ID_W_NAME);
                    if (!ObjectUtils.isEmpty(labels_w)) {
                        breed.setLabel_w(labels_w.get(0));
                        info_b.append(Constant.SEX_WOMAN + types[App.select_label_type] + "(编号：" + labels_w.get(0).getName() + ")");
                        info_n.append(Constant.SEX_WOMAN + types[App.select_label_type] + "(编号：" + labels_w.get(0).getName() + ")");
                    }
                    List<Label> labels_m = tablesDao.queryLabelForBreed(breed, LabelBreed.LABEL_ID_M_NAME);
                    if (!ObjectUtils.isEmpty(labels_m)) {
                        breed.setLabel_m(labels_m.get(0));
                        info_n.append("与").append(Constant.SEX_MAN + types[App.select_label_type] + "(编号：" + labels_m.get(0).getName() + ")");
                    }
                    if (breed.isBirth_time_remind()) {//预产
                        recordList.add(new RemindEntity(RemindEntity.TYPE_B, info_b.append("进行接生哦！").toString(),
                                "预产", TimeUtils.string2Millis(breed.getBirth_time()), breed));
                    }
                    if (breed.isNext_time_remind()) {//配种
                        recordList.add(new RemindEntity(RemindEntity.TYPE_B, info_n.append("进行二次配种哦！").toString(),
                                "配种", TimeUtils.string2Millis(breed.getNext_time()), breed));
                    }
                }

            }
            ForeignCollection<Vaccine> vaccines = pasture.getVaccines();
            if (null != vaccines && vaccines.size() > 0) {
                for (Vaccine vaccine : vaccines) {
                    //创建StringBuffer对象
                    StringBuffer info = new StringBuffer("请记得为");
                    //查询防治个体
                    List<Label> labels = tablesDao.queryLabelForVaccine(vaccine);
                    if (!ObjectUtils.isEmpty(labels)) {
                        vaccine.setLabel(labels.get(0));
                        info.append("(编号：" + labels.get(0).getName() + ")牲畜进行二次'" + vaccine.getType() + "'防治哦！");
                        if (vaccine.isNext_time_remind())
                            recordList.add(new RemindEntity(RemindEntity.TYPE_V, info.toString(),
                                    "防治", TimeUtils.string2Millis(vaccine.getNext_time()), vaccine));
                    }
                    //查询防治分群
                    List<Group> groups = tablesDao.queryGroupForVaccine(vaccine);
                    if (!ObjectUtils.isEmpty(groups)) {
                        vaccine.setGroup(groups.get(0));
                        info.append("'" + groups.get(0).getName() + "'进行二次'" + vaccine.getType() + "'防治哦！");
                        if (vaccine.isNext_time_remind())
                            recordList.add(new RemindEntity(RemindEntity.TYPE_V, info.toString(),
                                    "防治", TimeUtils.string2Millis(vaccine.getNext_time()), vaccine));
                    }
                }
            }
        }
        return recordList;
    }
}
